<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>设备管理</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 探头波形曲线调试模式显示区域 -->
    <el-card v-show="chartsShow">
      <el-row>
        <el-col :span="22">
          <el-form>
            <el-form-item label="调试模式:">
              <el-switch v-model="debugOn" @change="switchDebug($event)" active-color="#13ce66" active-text="开" inactive-text="关"></el-switch>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="2">
          <!-- <el-button type="primary" size="mini" @click="chartsShow = false">隐藏</el-button> -->
          <el-button icon="el-icon-arrow-up" circle @click="chartsShow = false"></el-button>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="18">
          <!-- 波形曲线区域 -->
          <div style="width: 800px; height: 500px" ref="waveChartsRef"></div>
        </el-col>
        <el-col :span="6">
          <el-form label-position="left" size="mini" label-width="80px" :disabled="!debugOn">
            <el-form-item label="闸门起始"><el-input v-model="setupData.start" size="mini"></el-input></el-form-item>
            <el-form-item label="闸门宽度"><el-input v-model="setupData.width" size="mini"></el-input></el-form-item>
            <el-form-item label="扫查范围"><el-input v-model="setupData.scanRange" size="mini"></el-input></el-form-item>
            <el-form-item label="探头增益"><el-input v-model="setupData.gain" size="mini"></el-input></el-form-item>
            <el-form-item>
              <el-button size="mini" type="primary" @click="refreshParams(waveDeviceId)">刷新当前参数</el-button>
              <el-button size="mini" type="primary" @click="setParams">设置参数</el-button>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
    </el-card>

    <!-- 卡片视图区域 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入检索的关键词" v-model="queryInfo.query" size="small" clearable @keypress.enter.native="getProbes"></el-input>
        </el-col>
        <el-col :span="8">
          <el-button type="primary" @click="getProbes" size="small">搜索</el-button>
        </el-col>
      </el-row>

      <!-- 探头清单表格区域 -->
      <el-table :data="devices">
        <el-table-column type="index" label="序号" width="80"> </el-table-column>
        <el-table-column prop="gateway" label="网关" width="150"> </el-table-column>
        <el-table-column prop="devicename" label="设备号" width="150"> </el-table-column>
        <el-table-column prop="probe" label="探头" width="150"> </el-table-column>
        <el-table-column prop="tube_name" label="管件名称" width="150"> </el-table-column>
        <el-table-column prop="tube_nominal" label="管件名义壁厚(mm)" width="200"> </el-table-column>
        <el-table-column label="操作" width="50">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="editDevice(scope.row.id)"></el-button>
          </template>
        </el-table-column>
        <el-table-column label="数据曲线" width="70">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-data-line" size="mini" @click="showCharts(scope.row.id)"></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页组件区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[20, 50, 100, 200]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 50,
      },
      setupData: {
        start: 0,
        width: 0,
        scanRange: 0,
        gain: 0,
      },
      total: 10,
      devices: [],
      // 需要调试的deviceId
      waveDeviceId: null,
      debugOn: false,
      chartsShow: false,
      waveCharts: null,
      waveData: [],
      xData: [],
      interval: null,
    }
  },
  created() {
    this.getProbes()
  },
  methods: {
    //   根据关键词检索探头列表
    async getProbes() {
      const { data: res } = await this.$http.get('thickcharts/searchprobes/', { params: this.queryInfo })
      if (res.meta.status === 200) {
        this.devices = res.data.devices
        this.queryInfo.pagenum = res.data.pagenum
        this.total = res.data.total
        // console.log(res)
        this.$message.success('查询探头列表成功！')
      } else {
        this.$message.error('查询探头列表失败，请联系管理员')
      }
    },
    // 根据deviceId获取探头的调试曲线及当前探头的配置参数
    async getWaveData(deviceId) {
      const { data: res } = await this.$http.get('thickcharts/getwave/', { params: { deviceId: deviceId } })
      // console.log(res)
      if (res.meta.status === 200) {
        this.waveData = res.data.wave
        this.xData = res.data.x_data
        // this.setupData.start = res.data.start
        // this.setupData.width = res.data.width
        // this.setupData.scanRange = res.data.scan_range
        // this.setupData.gain = res.data.gain
        this.updateCharts()
      }
    },
    // 刷新当前参数
    // 根据deviceId获取探头的调试曲线及当前探头的配置参数
    async refreshParams(deviceId) {
      const { data: res } = await this.$http.get('thickcharts/getwave/', { params: { deviceId: deviceId } })
      // console.log(res)
      if (res.meta.status === 200) {
        // this.waveData = res.data.wave
        // this.xData = res.data.x_data
        this.setupData.start = res.data.start
        this.setupData.width = res.data.width
        this.setupData.scanRange = res.data.scan_range
        this.setupData.gain = res.data.gain
      }
    },

    // 根据设备的id编辑设备信息,待完善
    editDevice(deviceId) {
      console.log(deviceId)
    },
    // 显示探头的调试的卡片试图
    showCharts(deviceId) {
      this.chartsShow = true
      this.waveDeviceId = deviceId
      this.getWaveData(deviceId)
      this.initWaveCharts()
    },
    // 初始化探头波形曲线
    initWaveCharts() {
      this.waveCharts = this.$echarts.init(this.$refs.waveChartsRef)
      var initOption = {
        xAxis: {
          type: 'category',
          data: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
        },
        yAxis: {
          type: 'value',
        },
        series: [
          {
            data: [10, 10, 10, 10, 10, 10, 10, 10, 10, 10],
            type: 'line',
            smooth: true,
            lineStyle: {
              normal: {
                width: 1,
              },
            },
          },
        ],
        tooltip: {
          trigger: 'axis',
          formatter: '{b}: {c}mm',
        },
        dataZoom: [
          {
            type: 'slider',
            xAxisIndex: 0,
          },
        ],
        toolbox: {
          feature: {
            saveAsImage: {},
            dataZoom: {},
            dataView: {
              show: true,
              title: '数据视图',
              readOnly: true,
            },
          },
        },
      }
      this.waveCharts.setOption(initOption)
    },
    // 请求回wave数据后更新charts
    updateCharts() {
      var updateOption = {
        xAxis: {
          data: this.xData,
        },
        series: [
          {
            data: this.waveData,
            areaStyle: {},
          },
        ],
      }
      this.waveCharts.setOption(updateOption)
    },
    // 切换debug状态
    async switchDebug(state) {
      // console.log(state)
      if (state) {
        console.log('开启debug 模式')
        const { data: res } = await this.$http.get('/thickcharts/debug/', { params: { state: 'on', deviceId: this.waveDeviceId } })
        if (res.meta.status === 200) {
          this.$message.success('开启调试模式成功！')
          // 每2s刷新一次波形数据
          this.interval = setInterval(() => {
            this.getWaveData(this.waveDeviceId)
          }, 2000)
        } else {
          this.$message.error('开启调试模式失败，请联系管理员！')
          this.debugOn = false
        }
      } else {
        const { data: res } = await this.$http.get('thickcharts/debug/', { params: { state: 'off', deviceId: this.waveDeviceId } })
        if (res.meta.status === 200) {
          // 关闭波形数据刷新
          clearInterval(this.interval)
          this.$message.success('关闭调试模式成功！')
        } else {
          this.$message.error('关闭调试模式失败，请联系管理员！')
          this.debugOn = true
        }
      }
    },
    // 当前页面变化处理函数
    handleCurrentChange(currentPage) {
      this.queryInfo.pagenum = currentPage
      this.getProbes()
    },
    // 当前页码变化
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getProbes()
    },
    // 为壁厚数据解析需要的闸门设置参数
    // 需要完善
    async setParams() {
      this.setupData.deviceId = this.waveDeviceId
      const { data: res } = await this.$http.post('thickcharts/setparams/', this.setupData)
      if (res.meta.status === 200) {
        this.$message.success('设置参数成功！')
        this.getWaveData(this.waveDeviceId)
      } else {
        this.$message.error('设置参数失败，请联系管理员！')
      }
    },
  },
}
</script>

<style lang='less' scoped>
</style>